<template>
	<el-dialog title="字段管理" v-model="visible" style="min-width: 80vw" :close-on-click-modal="false" draggable>
		<div class="layout-padding-auto layout-padding-view">
			<el-row v-show="showSearch">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item label="显示名称" prop="name">
						<el-input placeholder="请输入显示名称" v-model="state.queryForm.name" />
					</el-form-item>
					<el-form-item label="字段名称" prop="label">
						<el-input placeholder="请输入字段名称" v-model="state.queryForm.label" />
					</el-form-item>
					<!--      <el-form-item label="实体id" prop="entityId" >-->
					<!--        <el-input placeholder="请输入实体id" v-model="state.queryForm.entityId" />-->
					<!--      </el-form-item>-->
					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList"> 查 询</el-button>
						<el-button icon="Refresh" @click="resetQuery">重 置</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button
						icon="folder-add"
						type="primary"
						class="ml10"
						@click="formDialogRef.openDialog(null, state.queryForm.entityId)"
						v-auth="'admin_metaField_add'"
					>
						新 增
					</el-button>
					<el-button plain icon="upload-filled" type="primary" class="ml10" @click="excelUploadRef.show()" v-auth="'sys_user_add'"> 导 入 </el-button>
					<el-button plain :disabled="multiple" icon="Delete" type="primary" v-auth="'admin_metaField_del'" @click="handleDelete(selectObjs)">
						删 除
					</el-button>
					<el-button
						plain
						icon="DeleteFilled"
						type="primary"
						v-auth="'admin_metaField_del'"
						@click="recycleBinFormDialogRef.openDialog(state.queryForm.entityId)"
					>
						回收站
					</el-button>
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'admin_metaField_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" :selectable="selectable" align="center" />
				<el-table-column type="index" label="#" width="40" />
				<!--				<el-table-column prop="entityId" label="实体id" show-overflow-tooltip />-->
				<el-table-column prop="name" label="名称" show-overflow-tooltip />
				<el-table-column prop="label" label="编码" show-overflow-tooltip />
				<el-table-column prop="type" label="类型" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="field_type" :value="scope.row.type"></dict-tag>
					</template>
				</el-table-column>
				<el-table-column prop="nullable" label="是否为空" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_type" :value="scope.row.nullable"></dict-tag>
					</template>
				</el-table-column>
				<el-table-column prop="creatable" label="新增是否修改" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_type" :value="scope.row.creatable"></dict-tag>
					</template>
				</el-table-column>
				<el-table-column prop="updatable" label="更新是否修改" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_type" :value="scope.row.updatable"></dict-tag>
					</template>
				</el-table-column>
				<!--				<el-table-column prop="parentidFieldFlag" label="是否父表id标识" show-overflow-tooltip>-->
				<!--					<template #default="scope">-->
				<!--						<dict-tag :options="yes_no_type" :value="scope.row.parentidFieldFlag"></dict-tag>-->
				<!--					</template>-->
				<!--				</el-table-column>-->
				<el-table-column prop="lineFlag" label="是否换行" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_type" :value="scope.row.lineFlag"></dict-tag>
					</template>
				</el-table-column>

				<el-table-column prop="createTime" label="创建时间" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="updateTime" label="更新时间" sortable="custom" show-overflow-tooltip />
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button
							icon="edit-pen"
							:disabled="scope.row.systemFieldFlag == 1"
							text
							type="primary"
							v-auth="'admin_metaField_edit'"
							@click="formDialogRef.openDialog(scope.row.id)"
							>编辑
						</el-button>
						<el-button
							icon="delete"
							:disabled="scope.row.systemFieldFlag == 1"
							text
							type="primary"
							v-auth="'admin_metaField_del'"
							@click="handleDelete([scope.row.id])"
							>删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog style="position: fixed" ref="formDialogRef" @refresh="getDataList(false)" />
		<recycle-bin-form-dialog style="position: fixed" ref="recycleBinFormDialogRef" @refresh="getDataList(false)" @close="getDataList" />

		<!-- 导入excel (需要在 upms-biz/resources/file 下维护模板) -->
		<upload-excel
			ref="excelUploadRef"
			title="导入"
			url="/admin/metaField/import"
			temp-url="/admin/sys-file/local/file/metaField.xlsx"
			@refreshDataList="getDataList"
		/>
	</el-dialog>
</template>

<script setup lang="ts" name="systemMetaField">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/admin/metaField';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useDict } from '/@/hooks/dict';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const RecycleBinFormDialog = defineAsyncComponent(() => import('./recycleBin.vue'));
// 定义查询字典

const { yes_no_type, field_type } = useDict('yes_no_type', 'field_type');
// 定义变量内容
const visible = ref(false);
const loading = ref(false);
const formDialogRef = ref();
const recycleBinFormDialogRef = ref();
const excelUploadRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		delFlag: 0,
	},
	pageList: fetchList,
	createdIsNeed: false,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/admin/metaField/export', Object.assign(state.queryForm, { ids: selectObjs }), 'metaField.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('放入回收站');
	} catch {
		return;
	}

	try {
		await delObjs(ids, 0);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

const openDialog = (id: string) => {
	state.queryForm.entityId = id;
	getDataList();
	visible.value = true;
};
// 暴露变量
defineExpose({
	openDialog,
});

const selectable = (row) => {
	return row.systemFieldFlag != 1;
};
</script>
